package me.cxlr.qinlauncher2.view.launcher;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;

import androidx.fragment.app.FragmentActivity;

import me.cxlr.qinlauncher2.MyApplication;

/**
 * @author 酷安用户:山轮纸
 */
public class FlashLight {
    private volatile static FlashLight flashLight;

    private FlashLight() {
    }

    public static FlashLight getInstance() {
        if (flashLight == null) {
            synchronized (FlashLight.class) {
                if (flashLight == null) {
                    flashLight = new FlashLight();
                }
            }
        }
        return flashLight;
    }

    private boolean torch = false;

    /**
     * 使用闪光灯
     *
     */
    public void useFlashLight() {
        CameraManager cameraManager;
        if (torch) {
            try {
                cameraManager = (CameraManager) MyApplication.getInstance().getSystemService(Context.CAMERA_SERVICE);
                // 主闪光灯
                cameraManager.setTorchMode("0", true);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
            torch = false;
        } else {
            try {
                cameraManager = (CameraManager) MyApplication.getInstance().getSystemService(Context.CAMERA_SERVICE);
                cameraManager.setTorchMode("0", false);
            } catch (Exception e) {
                e.printStackTrace();
            }
            torch = true;
        }
    }
}
